/*
 *  Copyright (c)  2016-NOW  Jacars Co.Ltd.
 *  All rights reserved.
 */
package com.jacars.biz.user.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jacars.biz.user.dao.model.JsptTenant;
import com.jacars.biz.user.dao.service.JsptTenantService;
import com.jacars.biz.user.service.TenantService;
import com.jacars.common.context.TenantContextHolder;
import com.jacars.common.utils.support.Kv;
import com.jacars.common.utils.FuncUtil;
import com.jacars.db.support.Condition;
import com.jacars.db.support.Query;
import com.jacars.common.annotation.LoginUser;
import com.jacars.common.constant.CommonConstant;
import com.jacars.common.api.PageResult;
import com.jacars.common.api.Result;
import com.jacars.common.model.JsptUser;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tenant")
@ApiIgnore
@Api(value = "租户管理", tags = "接口")
public class TenantController {

	private TenantService tenantService;

	private JsptTenantService jsptTenantService;




	/**
	 * 自定义分页
	 */
	@GetMapping("/tenantPage")
	@ApiOperation(value = "分页", notes = "传入tenant")
	public PageResult<JsptTenant> tenantPage(JsptTenant jsptTenant, Query query, @LoginUser(isFull = true) JsptUser jsptUser) {
		QueryWrapper<JsptTenant> queryWrapper = Condition.getQueryWrapper(jsptTenant);
		IPage<JsptTenant> pages = jsptTenantService.page(Condition.getPage(query),
				(!jsptUser.getTenantId().equals(CommonConstant.ADMIN_TENANT_ID))
						? queryWrapper.lambda().eq(JsptTenant::getTenantId, jsptUser.getTenantId())
						: queryWrapper);
		List<JsptTenant> records = pages.getRecords();

		return PageResult.succeed(records, pages.getTotal());
	}

	/**
	 * 分页, select 统一到 list
	 */
	@GetMapping("/tenantList")
	@ApiOperation(value = "分页", notes = "传入tenant")
	public Result<List<JsptTenant>> tenantList(JsptTenant jsptTenant, @LoginUser(isFull = true) JsptUser jsptUser) {
		QueryWrapper<JsptTenant> queryWrapper = Condition.getQueryWrapper(jsptTenant);
		List<JsptTenant> list = jsptTenantService.list(
				(!jsptUser.getTenantId().equals(CommonConstant.ADMIN_TENANT_ID))
						? queryWrapper.lambda().eq(JsptTenant::getTenantId, jsptUser.getTenantId())
						: queryWrapper);

		return Result.succeed(list);

	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/create")
	@ApiOperation(value = "新增或修改", notes = "传入tenant")
	public Result submit(@Valid @RequestBody JsptTenant jsptTenant) {
		return Result.status(tenantService.saveTenant(jsptTenant));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public Result remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return Result.status(jsptTenantService.removeByIds(FuncUtil.toLongList(ids)));
	}


	/**
	 * 新增或修改
	 */
	@PostMapping("/update")
	@ApiOperation(value = "新增或修改", notes = "传入tenant")
	public Result update(@Valid @RequestBody JsptTenant jsptTenant) {
		return Result.status(jsptTenantService.updateById(jsptTenant));
	}

	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入tenant")
	public Result<JsptTenant> detail(@PathVariable Long id) {
		JsptTenant detail = jsptTenantService.getById(id);
		return Result.succeed(detail);
	}

	/**
	 * 根据域名查询信息
	 *
	 * @param domain 域名
	 */
	@GetMapping("/info")
	@ApiOperation(value = "配置信息", notes = "传入domain")
	public Result<Kv> info(String domain) {
		JsptTenant jsptTenant = jsptTenantService.getOne(Wrappers.<JsptTenant>query().lambda().eq(JsptTenant::getDomain, domain));
		Kv kv = Kv.init();
		if (jsptTenant != null) {
			kv.set("tenantId", jsptTenant.getTenantId()).set("domain", jsptTenant.getDomain());
		}
		return Result.succeed(kv);
	}

	/**
	 * 详情
	 */
	@GetMapping("/getByTenantId")
	@ApiOperation(value = "详情", notes = "传入tenant")
	public Result<JsptTenant> getByTenantId() {

		String tenant = TenantContextHolder.getTenant();
		JsptTenant detail = jsptTenantService.getOne(Wrappers.<JsptTenant>query().lambda().eq(JsptTenant::getTenantId, tenant));
		return Result.succeed(detail);
	}

}
